

package cuttings;

import java.io.*;
import java.net.*;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;


public class ServerConnect implements Runnable
{
    String userName;
    String password;
    String email;
    int mode;
    int welcomeport;
    TCPClient obj;
    static DataOutputStream sendToServer;
    static String fbToken;
    
    public ServerConnect(String name,String pass,int port,TCPClient object,String mail,int mod)
    {
        System.out.println("in SC constrctor:"+name+","+pass+","+mail+","+mod);

        userName = name;
        password = pass;
        welcomeport = port;
        obj = object;
        email = mail;
        mode = mod;
        System.out.println("in SC constrctor2:"+userName+","+password+","+email+","+mode);
    }

    public void run()
    {
	try {
	    connectToServer();
	} catch (Exception e) {
	    System.out.println(e);
	}
    }

    public void connectToServer() throws Exception
    {
        String ConnectSentence = "";
        String usersInfo;

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket ServerConnectionSocket = new Socket("172.17.190.19", 6789);
        DataOutputStream outToServer = new DataOutputStream(ServerConnectionSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(ServerConnectionSocket.getInputStream()));

        sendToServer = outToServer;

        //sentence = inFromUser.readLine();
        System.out.println("pass:"+password);
        //ConnectSentence = "login " + userName +","+ password;
        //outToServer.writeBytes(ConnectSentence + '\n');

        String encpassword = encryptPassword(password);

        if(mode==1)
           ConnectSentence = "login " + userName +","+ encpassword;
        else if(mode==2)
            ConnectSentence = "newuser "+userName+","+encpassword+","+email;
        else if(mode==3)
            ConnectSentence = "password "+userName;
            

        outToServer.writeBytes(ConnectSentence + '\n');
         System.out.println("Written to server:"+ConnectSentence);
        
        while(true)
        {
            System.out.println("waiting for server");
            usersInfo = inFromServer.readLine();
            if(usersInfo.equals("invalid"))
            {
                //System.out.println("invalid user");
                TCPClient.server_return = "invalid";
                break;
            }
            System.out.println("FROM SERVER: " + usersInfo);

            if(usersInfo.startsWith("adduser")||usersInfo.startsWith("removeuser"))
                PeerInfoList.processPeersInfo(usersInfo,userName);

            //System.out.println("in stage 2");

            if(usersInfo.startsWith("addhost")||usersInfo.startsWith("removehost"))
                UserRole.constructHostList(usersInfo);

            if(usersInfo.startsWith("sethost")) //sethost,hostname,status(string)
                ;

            if(usersInfo.startsWith("Alive"))
                sendToServer("Yes");

            if(usersInfo.startsWith("token:"))
            {
                String temp[] = usersInfo.split(":");
                fbToken = temp[1];
                System.out.println("FBToken:"+fbToken);
            }

            //if(usersInfo.equals("newuserpassed"))
            //    Menu.newUserStatus(1);

            //if(usersInfo.equals("newuserfailed"))
            //    Menu.newUserStatus(0);


            //System.out.println("in stage 3");
            //obj.notify();
        }

        //ServerConnectionSocket.close();//if u close this u ll get disconnected from server

       //return usersInfo; //temp.name + "," + temp.hostName + "," + temp.hostAddress  + "\n";
    }

    public static String encryptPassword(String pass)
    {
        String myText = pass;
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("jasypt");                     // we HAVE TO set a password
        encryptor.setAlgorithm("PBEWithMD5AndTripleDES");    // optionally set the algorithm
        String encryptedText = encryptor.encrypt(myText);

        return encryptedText;
    }

    public static void sendToServer(String message) throws Exception
    {
        sendToServer.writeBytes(message+"\r\n");
        System.out.println("Sent to Server:"+message);
    }

    public void clickedHostGame() throws Exception
    {
        sendToServer("host"+userName);
        TCPClient.iAmHost = true;
    }

    public void clickedStartGame() throws Exception
    {
        sendToServer("start"+userName);
    }

    public void clickedStopGame() throws Exception
    {
        sendToServer("stop"+userName);
    }
}
